<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * User Application (User), User List Module
 * Started: 19 September, 2009 09:14 PM
 * Script version: 1.0
 +------------------------------------
 */

class module implements module_interface{
	public function returninitvars(){
		global $lang;
		$this->info["page_title"] = $lang->getByKey("admin.user.user.list");
		$this->info["page_description"] = "";
		$this->info["module_version"] = "1.0";
		return $this->info;
	}

	public function construct(){ # call our initialize functions
		$this->page = (isset($_GET['page']) ? intval($_GET['page']) : 1);
	}
	
	private function can_delete($data, $groupdata){
		$nodelete = array();
		$nodelete[] = "1";
		$uc = unserialize($_SESSION["cf_controller"]);
		$nodelete[] = $uc->getUID();
		// Now...
		if(in_array($data['id'], $nodelete)){
			return false;
		}
		else{
			if($groupdata->is_admin()){
				return false;
			}
			else{
				return true;
			}
		}
	}
	
	public function run(){
		global $_CF;
		$_CF->view->writeIn( $_CF->view->inlineInfo($_CF->lang->getByKey("admin.user.user.list.desc")));
		// Pre-Query Them.
		$userQuery = $_CF->models->users->select_all();
		$this->numberOfUsers = $_CF->db->num_rows($userQuery);
		$this->numberOfPages = ceil($this->numberOfUsers/4);
		// Gotta use columns extensively.
		// So first we have to set the pointer to where we *actually* want.
		$x = 0;
		while($x !== ($this->page-1)*4 and $array = $_CF->db->fetch_array($userQuery)){ $x++; }
		$i = 0;
		$o = 0; // so many temp vars... confused. :(
		while(($data = $_CF->db->fetch_array($userQuery)) !== false and $i !== 4){
			// Pointy-pointer
			$i++;
			// W-ait! Gotta get group-y!
			// Caching the controllers are so fun
			if(!isset($group{$data['group']})){
				$group{$data['group']} = new group($data['group']);
			}
			$data['groupname'] = $group{$data['group']}->getName();
			$data2['del'] = ($this->can_delete($data, $group{$data['group']}) ? $_CF->view->showTemplate("list_del_ls", $data) : 
				$_CF->view->showTemplate("list_del_disabled", $data));
			// After this infinite loop is over
			$data['del'] = $data2['del'];
			$output = $_CF->view->tablePadded($data["username"],
									  $_CF->view->showTemplate("usercard", $data),
									  "user");
			if(!isset($columns[$o])){
				$columns[$o]["left"] = $output;
			}
			else{
				if(isset($columns[$o]["left"]) and !isset($columns[$o]["right"])){
					$columns[$o]["right"] = $output;
					$o++;
				}
			}
		}
		// Now yay - Output it.
		for($d=0;($d-1)!==$o;$d++){
			if(!isset($columns[$d]["right"])) $columns[$d]["right"] = "";
			if(!isset($columns[$d]["left"])) $columns[$d]["left"] = ""; // Ugly workaround to get rid of errors.
			$_CF->view->writeIn($_CF->view->showTemplate("column", $columns[$d]));
		}
		// So pagination.
		if($this->numberOfPages > 1){
			$this->pagenav = "";
			if($this->page > 1){ # previous button
				$this->pagenav .= $_CF->view->showTemplate("pagebuttonu", array("data" => $_CF->lang->getByKey("page.prev"),
				"page" => ($this->page-1)));
			}
			$_CF->view->writeIn(
				$_CF->view->showTemplate("npages", array("pages" => $this->numberOfPages, "page" => $this->page)).
				$this->pagenav
			);
		}
	}

	public function destruct(){}
}
